#include "tm1650_driver.h"
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"

typedef union
{
    uint32_t Buffer;
    struct
    {
        /* 这里可以对每bit位进行操作 */
        uint16_t Speed_L : 16; //速度低
        uint16_t Speed_H : 16; //速度高
    }Bits;
}WHT_TM1650_Display_Buffer_t;

/* 真值表 */
static const uint16_t Speed_Table[] =
{
    0x3fe7,// 0
    0x3060,// 1
    0x0ff9,// 2
    0x3cf9,// 3
    0x307e,// 4
    0x3c9f,// 5
    0xff9f,// 6
    0x30e1,// 7
    0x3fff,// 8
    0x3cff,// 9
};


typedef enum
{
    Speed_Type,    //速度
    /* 待添加其它 */
    All_Type,      //所有
}WHT_Display_Type_enum;



static WHT_TM1650_Display_Buffer_t WHT_TM1650_Display_Buffer;

/* 设置显示状态 */
static void WHT_TM1650_Set_Status(WHT_Display_Type_enum type, uint32_t value)
{
    switch (type)
    {
    case Speed_Type:
        WHT_TM1650_Display_Buffer.Bits.Speed_L = value < 100 ? Speed_Table[value%10] : Speed_Table[9];
        WHT_TM1650_Display_Buffer.Bits.Speed_H = value < 100 ? Speed_Table[value/10] : Speed_Table[9];
        break;
    /* 待添加其它 */
    case All_Type:
        memset(&WHT_TM1650_Display_Buffer.Buffer, value, sizeof(WHT_TM1650_Display_Buffer.Buffer));
        break;
    }
}




void tm1650_main(void)
{
    uint8_t speed = 0;
    uint8_t keys_value;

    vTaskDelay(100);

    WHT_TM1650_Driver.WHT_Init();
    WHT_TM1650_Set_Status(All_Type, 0);
    WHT_TM1650_Driver.WHT_Set_Display_Value(WHT_TM1650_Display_Buffer.Buffer);
    WHT_TM1650_Driver.WHT_Set_Brightness(TM1650_Brightness_5);
    
    for (; ;)
    {
        WHT_TM1650_Set_Status(Speed_Type, speed++);
        WHT_TM1650_Driver.WHT_Set_Display_Value(WHT_TM1650_Display_Buffer.Buffer);
        if (speed >= 100)
        {
            speed = 0;
        }
        keys_value = WHT_TM1650_Driver.WHT_Get_Kyes_Value();
        printf("keys_value = 0x%X\r\n", keys_value);

        vTaskDelay(333);
    }
}

